﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.IO;

namespace Tesla.Content {
    /// <summary>
    /// A resource that is represented by a single file on the disk.
    /// </summary>
    public class FileResource : IResource {
        private FileInfo _info;
        private String _extension;
        private String _name;
        private String _fullName;

        /// <summary>
        /// Gets the file extension.
        /// </summary>
        public String Extension {
            get {
                return _extension;
            }
        }

        /// <summary>
        /// Get the resource's file name (without extension).
        /// </summary>
        public String Name {
            get {
                return _name;
            }
        }

        /// <summary>
        /// Get the fully qualified path to the resource (including name and extension).
        /// </summary>
        public String FullName {
            get {
                return _fullName;
            }
        }

        /// <summary>
        /// Creates a new file resource from the fully qualified name.
        /// </summary>
        /// <param name="fullFileName">Fully qualified name of file</param>
        public FileResource(String fullFileName) {
            FileInfo info = new FileInfo(fullFileName);
            _info = info;
            _name = Path.GetFileNameWithoutExtension(fullFileName);
            _fullName = _info.FullName;
            _extension = _info.Extension.ToLower();
        }

        /// <summary>
        /// Opens a stream that is read-only.
        /// </summary>
        /// <returns>File stream</returns>
        /// <exception cref="System.IO.IOException">Thrown if file does not exist.</exception>"
        public Stream OpenStream() {
            if(_info.Exists) {
                return _info.OpenRead();
            } else {
                throw new IOException("File does not exist");
            }
        }

    }
}
